package ar.uba.dc.cmovil.tripplanner.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import ar.uba.dc.cmovil.tripplanner.R;
import ar.uba.dc.cmovil.tripplanner.domain.Trip;
import ar.uba.dc.cmovil.tripplanner.utils.DateFormatter;

/**
 * Created by anton on 9/28/13.
 */
public class TripItemWidget extends LinearLayout {

    private final Trip trip;
    private final Button deleteButton;
    private final TextView tripName;
    private final TextView startDate;
    private TripItemEventsHandler eventsHandler;

    public TripItemWidget(Context context, Trip trip) {
        super(context);

        this.trip = trip;
        this.setOrientation(HORIZONTAL);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.trip_widget_view, this, true);


        this.tripName = (TextView) getChildAt(0);
        this.startDate = (TextView) getChildAt(1);
        this.deleteButton = (Button) getChildAt(2);

        this.tripName.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (eventsHandler != null) {
                    eventsHandler.onTripClicked(TripItemWidget.this.trip);
                }
            }
        });

        this.deleteButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (eventsHandler != null) {
                    eventsHandler.onTripDeleteClicked(TripItemWidget.this.trip);
                }
            }
        });

        setTripName(trip.getName());
        if (trip.getStartDate() != null) {
            setStartDate(DateFormatter.formatDate(trip.getStartDate()));
        }
    }

    public void setTripEventsHandler(TripItemEventsHandler handler) {
        eventsHandler = handler;
    }

    public void setTripName(String tripName) {
        this.tripName.setText(tripName);
    }

    public void setStartDate(String startDate) {
        this.startDate.setText(startDate);
    }

    public interface TripItemEventsHandler {
        void onTripClicked(Trip trip);

        void onTripDeleteClicked(Trip trip);
    }
}
